﻿using System;
using System.Data;
using System.Configuration;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace BingoLib
{
/// <summary>
/// DrawingCurve 的摘要说明
/// </summary>
    public class DrawingCurve
    {
        #region Atrributes

        #region Image

        /// <summary>
        /// 图片尺寸。宽、高
        /// </summary>
        public Size ImgSize = new Size(500, 1000);
        /// <summary>
        /// 图片边距。上、右、下、左
        /// </summary>
        public int[] Distance = new int[] { 15, 50, 15, 50 };

        #endregion

        #region Title

        /// <summary>
        /// 图像标题
        /// </summary>
        public string Title = "";
        /// <summary>
        /// 图像标题颜色
        /// </summary>
        public Color TitleColor = Color.Red;
        /// <summary>
        /// 图像标题区高度
        /// </summary>
        public int TitleHeight = 50;
        /// <summary>
        /// 图像标题起始坐标
        /// </summary>
        public Point TitleLocation = new Point(50, 0);
        /// <summary>
        /// 图像标题是否位于顶部
        /// </summary>
        public bool TitleInTop = true;
        /// <summary>
        /// 图像标题Font属性
        /// </summary>
        public Font TitleFont = new Font("宋体", 15, FontStyle.Bold);

        #endregion

        #region Chart

        /// <summary>
        /// 柱形图颜色
        /// </summary>
        public Color RecColor = Color.Black;
        /// <summary>
        /// 柱形图宽度
        /// </summary>
        public int RecWidth = 20;
        /// <summary>
        /// 柱形图微调参数，右偏移大小，负数为左移 
        /// </summary>
        public int RecDisLeft = 0;

        #endregion

        #region Mark

        /// <summary>
        /// 柱形图上下方文字高度
        /// </summary>
        public int MarkHeight = 20;
        /// <summary>
        /// 柱形图上方文字颜色
        /// </summary>
        public Brush MarkUpColor = Brushes.Red;
        /// <summary>
        /// 柱形图上方文字微调参数
        /// </summary>
        public int MarkUpLeft = 0;
        /// <summary>
        /// 柱形图下方文字颜色
        /// </summary>
        public Brush MarkDownColor = Brushes.Black;
        /// <summary>
        /// 柱形图下方文字微调参数
        /// </summary>
        public int MarkDownLeft = 0;

        #endregion

        #region DataBase

        /// <summary>
        /// 数据源，当前支持两个字段
        /// </summary>
        public DataTable Table;

        #endregion

        #endregion

        public DrawingCurve()
        {
        }

        public Bitmap DrawingImg()
        {
            int DistTop = Distance[0];
            int DistBottom = Distance[2];
            if (TitleInTop) DistTop = Distance[0] + TitleHeight;
            else DistBottom = Distance[2] + TitleHeight;

            Bitmap image = new Bitmap(ImgSize.Width, ImgSize.Height);
            //创建Graphics类对象
            Graphics g = Graphics.FromImage(image);

            try
            {
                //清空图片背景色
                g.Clear(Color.White);
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), TitleColor, Color.BlueViolet, 1.2f, true);
                g.FillRectangle(Brushes.WhiteSmoke, 0, 0, ImgSize.Width, ImgSize.Height);
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);

                //绘制标题
                g.DrawString(Title, TitleFont, brush, new Point(TitleLocation.X + Distance[3], TitleInTop ? (Distance[0] + TitleLocation.Y) : (ImgSize.Height - DistBottom + TitleLocation.Y)));
                
                Pen mypen = new Pen(brush, 2);
                //绘制底部横线，柱形位于横线上方，标识位于横线下方
                g.DrawLine(mypen, Distance[3], ImgSize.Height - DistBottom - MarkHeight, ImgSize.Width - Distance[1], ImgSize.Height - DistBottom - MarkHeight);

                //计算柱形图的间距
                float InterVal = (float)(ImgSize.Width - Distance[1] - Distance[3] - RecWidth) / (float)(Table.Rows.Count - 1);
                //数字与柱形图高度的比例
                int MaxCount = 0;
                foreach (DataRow dr in Table.Rows)
                {
                    if (MaxCount < int.Parse(dr[1].ToString())) MaxCount = int.Parse(dr[1].ToString());
                }
                float scale = ((float)(ImgSize.Height - Distance[0] - Distance[2] - MarkHeight * 2 - TitleHeight)) / ((float)MaxCount);

                SolidBrush mybrush = new SolidBrush(RecColor);
                //绘制柱形图和文字标识
                Font font = new Font("Arial", 10, FontStyle.Regular);
                for (int i = 0; i < Table.Rows.Count; i++)
                {
                    g.DrawString(Table.Rows[i][1].ToString(), font, MarkUpColor, InterVal * i + Distance[3] + MarkUpLeft, ImgSize.Height - DistBottom - MarkHeight * 2 - scale * (int.Parse(Table.Rows[i][1].ToString())));
                    g.FillRectangle(mybrush, InterVal * i + Distance[3] + RecDisLeft, ImgSize.Height - DistBottom - MarkHeight - scale * (int.Parse(Table.Rows[i][1].ToString())), RecWidth, scale * (int.Parse(Table.Rows[i][1].ToString())));
                    g.DrawString(Table.Rows[i][0].ToString(), font, MarkDownColor, InterVal * i + Distance[3] + MarkDownLeft, ImgSize.Height - DistBottom - MarkHeight);
                }
            }
            finally
            {
            }

            return image;
        }
    }
}